package com.neulogistics.core.customercenter.domain.model.enumeration;

import com.neulogistics.core.customercenter.domain.exception.CustomerCenterDomainException;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Enumeration;

import java.util.*;
import java.util.stream.Collectors;

public class OrderType extends Enumeration {

    public static final int Delivery_Id = 1;
    public static final int Return_Id = 2;
    public static final int Exchange_Id = 3;

    public static OrderType Delivery = new OrderType(Delivery_Id, "Delivery");
    public static OrderType Return = new OrderType(Return_Id, "Return");
    public static OrderType Exchange = new OrderType(Exchange_Id, "Replace");


    protected OrderType(int id, String name) {
        super(id, name);
    }

    private static List<OrderType> list = Arrays.asList(Delivery, Return, Exchange);

    public static List<OrderType> getList(){return list;}

    public static OrderType fromName(String name) throws CustomerCenterDomainException {
        Optional<OrderType> optional = list.stream().filter(p -> p.name.equals(name)).findFirst();

        if (optional.isPresent()){
            return optional.get();
        }
        else {
            throw new CustomerCenterDomainException(
                    ErrorCode.OPERATION_ERROR,
                    String.format("Possible values for {%s}: %s",
                            OrderType.class.getSimpleName(),
                            list.stream()
                                    .map(p -> p.name)
                                    .collect(Collectors.joining(",")))
            );
        }
    }

    @Override
    public int compareTo(Object o) {
        return 0;
    }
}
